﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Design;

namespace BPM.Base.WFLib.CustomerAttribute
{
    public  class ListBoxUIEditorBase : System.Drawing.Design.UITypeEditor
    {
        protected ListBox list;
        IWindowsFormsEditorService editorSsevice;
        public ListBoxUIEditorBase()
        {
            list = new ListBox(); 
            //list.Height = 40;
            list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
            list.BorderStyle = BorderStyle.None;
            this.FillCollection();
            

        }

        protected virtual void FillCollection()
        {
            ;
        }

        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }


        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            editorSsevice = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (editorSsevice != null)
            {

                editorSsevice.DropDownControl(list);

                if (list.SelectedItem == null)
                {
                    return value;
                }
                else
                {
                    return list.SelectedItem.ToString();
                }

            }
            return value;
        }

        void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            editorSsevice.CloseDropDown();
        }
    }

  



}
